package com.jhd.jiangxi.activity.scan.in.supply;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jhd.jiangxi.R;
import com.jhd.jiangxi.UtilHelper;
import com.jhd.jiangxi.activity.main.BaseActivity;
import com.jhd.jiangxi.service.GetVaccineService;
import com.jhd.jiangxi.service.VaccineService;
import com.jhd.jiangxi.vo.DeliveryDtl;
import com.jhd.jiangxi.vo.ElectCodePackageVo;
import com.jhd.jiangxi.vo.ElectCodePackageVoList;
import com.jhd.jiangxi.vo.JsonForResult;
import com.jhd.jiangxi.vo.PurchaseReceiptDtlVo;
import com.jhd.jiangxi.vo.PurchaseReceiptMstAndDtlVo;
import com.jhd.jiangxi.vo.PurchaseReceiptMstVo;
import com.jhd.jiangxi.vo.StorageInfo;

import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.jhd.jiangxi.activity.scan.in.supply.SupplyActivity.electCodePackageVos2;

public class SupplyStorageActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private ProgressDialog _progressDialog = null;
    private List<StorageInfo> list = new ArrayList<>();
    public static List<DeliveryDtl> deliveryDtlList = new ArrayList<>();
    public static List<ElectCodePackageVo> electCodePackageVos = new ArrayList<ElectCodePackageVo>();
    private Button btnCountVaccine, btnSubmitVaccine, btnDelAllVaccine;
    private RecyclerAdapter adapter;
    private AlertDialog _alertDialog = null;
    private static boolean isAll = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supply_storage);
        application.setPosition(-1);
        if ((electCodePackageVos.size() + electCodePackageVos2.size()) == 0&&!application.getOrderNoId().equals(application.getOrderNoId2())) {//做一个判断.如果俩个数组的size()和不为0,就不用再获取数据了
            application.setOrderNoId2(application.getOrderNoId());
            getDeliveryDtl();
        }
        initView();
        initData();
    }

    // 第二种
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Intent intent = new Intent("com.jhd.test.broadcast");
            intent.putExtra("type", "0");//0代表返回键
            sendBroadcast(intent);
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    protected void initHeader(String title) {
        if ("0".equals(application.getBackSystem())) title = title + "(测试版本)";
        TextView tvTitle = ((TextView) findViewById(R.id.screenTitle));
        if (tvTitle != null) tvTitle.setText(title);
        findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.jhd.test.broadcast");
                intent.putExtra("type", "0");//0代表返回键
                (v.getContext()).sendBroadcast(intent);
                onBackPressed();
            }
        });
        String userName = application.getRealName() == null ? application.getUserName() : application.getRealName();
        ((TextView) findViewById(R.id.tvHeaderUserName)).setText(userName);
    }

    private void initView() {
        initHeader("选择入库储位");
        recyclerView = (RecyclerView) findViewById(R.id.rlv_list);
        btnCountVaccine = (Button) findViewById(R.id.btnCountVaccine);
        btnSubmitVaccine = (Button) findViewById(R.id.btnSubmitVaccine);
        btnDelAllVaccine = (Button) findViewById(R.id.btnDelAllVaccine);
        btnCountVaccine.setOnClickListener(this);
        btnSubmitVaccine.setOnClickListener(this);
        btnDelAllVaccine.setOnClickListener(this);
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(manager);
    }

    private void initData() {
        _progressDialog = new ProgressDialog(SupplyStorageActivity.this);
        _progressDialog.setMessage("正在获取储位信息,请稍等...");
        _progressDialog.show();
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        params.put("deptId", application.getDeptId());
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.EMIAO(application.getBackSystem()));
        VaccineService vs = retrofit.create(VaccineService.class);
        String token = application.getAccessToken();
        Call<JsonForResult> jsonForResultCall = vs.getStorageInfoList(token, params);
        jsonForResultCall.enqueue(new getStorageInfoListCallback());
    }

    private void getDeliveryDtl() {
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        params.put("deptId", application.getDeptId());
        params.put("id", application.getOrderNoId());
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.EMIAO(application.getBackSystem()));
        VaccineService vs = retrofit.create(VaccineService.class);
        String token = application.getAccessToken();
        Call<JsonForResult> jsonForResultCall = vs.getDeliveryDtlByMstId(token, params);
        jsonForResultCall.enqueue(new getDeliveryDtlCallback());
    }

    private void getDelivery() {
        for (int i = 0; i < deliveryDtlList.size(); i++) {
            String category = deliveryDtlList.get(i).getCategory();
            String propertyId = deliveryDtlList.get(i).getPropertyId();
            String vaccineName = deliveryDtlList.get(i).getVaccineName();
            String manufacturerName = deliveryDtlList.get(i).getManufacturerName();
            String manufacturerId = deliveryDtlList.get(i).getManufacturerId();
            String id = deliveryDtlList.get(i).getId();
            String lotNumber = deliveryDtlList.get(i).getLotNumber();
            String vaccineDose = deliveryDtlList.get(i).getVaccineDose();
            String vaccineId = deliveryDtlList.get(i).getVaccineId();
            String vaccineLotsId = deliveryDtlList.get(i).getVaccineLotsId();
            String vaccineSpec = deliveryDtlList.get(i).getVaccineSpec();
            String storageId = deliveryDtlList.get(i).getStorageId();
            String storage = deliveryDtlList.get(i).getStorage();
            String expiryDate = deliveryDtlList.get(i).getExpiryDate();
            int unitPrice = deliveryDtlList.get(i).getUnitPrice();
            int servicePrice = deliveryDtlList.get(i).getServicePrice();
            List<ElectCodePackageVo> electCodePackageVoList = deliveryDtlList.get(i).getElectCodePackageVoList();

            for (int j = 0; j < electCodePackageVoList.size(); j++) {
                ElectCodePackageVo electCodePackageVo = deliveryDtlList.get(i).getElectCodePackageVoList().get(j);
                electCodePackageVo.setCategory(category);
                electCodePackageVo.setManufacturerName(manufacturerName);
                electCodePackageVo.setVaccineName(vaccineName);
                electCodePackageVo.setManufacturerId(manufacturerId);
                electCodePackageVo.setExpiryDate(expiryDate);
                electCodePackageVo.setId(id);
                electCodePackageVo.setPropertyId(propertyId);
                electCodePackageVo.setLotNumber(lotNumber);
                electCodePackageVo.setVaccineDose(vaccineDose);
                electCodePackageVo.setVaccineLotsId(vaccineLotsId);
                electCodePackageVo.setVaccineId(vaccineId);
                electCodePackageVo.setVaccineSpec(vaccineSpec);
                electCodePackageVo.setUnitPrice(unitPrice);
                electCodePackageVo.setServicePrice(servicePrice);
                electCodePackageVo.setStorageId(storageId);
                electCodePackageVo.setStorage(storage);
                electCodePackageVos.add(electCodePackageVo);
            }
        }
    }

    private class getDeliveryDtlCallback implements Callback<JsonForResult> {

        @Override
        public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
            try {
                JsonForResult jsonForResult = (JsonForResult) response.body();
                if (jsonForResult != null && jsonForResult.isSuccess() && jsonForResult.getData() != null) {
                    deliveryDtlList = JSON.parseArray(jsonForResult.getData().toString(), DeliveryDtl.class);
                    getDelivery();
                    application.setDeliveryDtl((JSONArray) jsonForResult.getData());
                    System.out.println("数据" + application.getDeliveryDtl());
                } else {
                    if (jsonForResult != null && jsonForResult.getMessage() != null && "".equals(jsonForResult.getMessage()) == false) {
                        UtilHelper.alertMsg(SupplyStorageActivity.this, "错误", jsonForResult.getMessage());
                    }
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<JsonForResult> call, Throwable t) {
            String msg = "网络异常!";
            if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
            UtilHelper.alertMsg(SupplyStorageActivity.this, "错误", msg);
        }
    }

    private void savePurchaseReceiptMstAndDtlFromSale() {
        Map<String, Object> params = new LinkedHashMap<String, Object>();
        PurchaseReceiptMstAndDtlVo purchaseReceiptMstAndDtlVo = new PurchaseReceiptMstAndDtlVo();
        PurchaseReceiptMstVo purchaseReceiptMstVo = new PurchaseReceiptMstVo();
        purchaseReceiptMstVo.setApprovalId(application.getApprovalId());
        purchaseReceiptMstVo.setArrivalTime(getDate(application.getArrivalTime()));
        purchaseReceiptMstVo.setCategory(electCodePackageVos2.get(0).getCategory());
        if(application.getColdChainType()!=null){//冷链类型
            int coldChainType = Integer.valueOf(application.getColdChainType());
            if (coldChainType > 10) {//小于10的时候,在前面补0
                purchaseReceiptMstVo.setColdChainType(application.getColdChainType());
            } else {
                purchaseReceiptMstVo.setColdChainType("0" + application.getColdChainType());
            }
        }
        purchaseReceiptMstVo.setDeptId(application.getDeptId());
        purchaseReceiptMstVo.setHandleDate(getTime(new Date()));
        purchaseReceiptMstVo.setHandlerName(application.getUserName());
        purchaseReceiptMstVo.setPriceBookNo(application.getPriceNoIn());
        purchaseReceiptMstVo.setPriceBookMstId(application.getPriceBookMstId());
        purchaseReceiptMstVo.setPurchaseType("2");//供应入库写2
        purchaseReceiptMstVo.setRemark(application.getRemark());//备注
        purchaseReceiptMstVo.setRunTime(getDate(application.getRunTime()));//
        purchaseReceiptMstVo.setSourceId(application.getOrderNoId());//获取订单里的id
        purchaseReceiptMstVo.setSupplyDeptId(application.getSupplyDeptId());
        purchaseReceiptMstVo.setSupplyDeptName(application.getSupplyDeptName());
        purchaseReceiptMstVo.setTransformUnit(application.getTransformId());
        purchaseReceiptMstVo.setTransformUnitName(application.getTransformName());
        purchaseReceiptMstVo.setUndefined(application.getOrderNoName());
        purchaseReceiptMstVo.setUserId(application.getUserId());
        purchaseReceiptMstVo.setUserName(application.getUserName());
//        purchaseReceiptMstVo.setSourceType("3");//sourceType为3代表来源类型是上级交货单
        purchaseReceiptMstAndDtlVo.setPurchaseReceiptMstVo(purchaseReceiptMstVo);
        List<PurchaseReceiptDtlVo> purchaseReceiptDtlVoList = new ArrayList<PurchaseReceiptDtlVo>();
        for (int i = 0; i < electCodePackageVos2.size(); i++) {
            PurchaseReceiptDtlVo purchaseReceiptDtlVo = new PurchaseReceiptDtlVo();
            purchaseReceiptDtlVo.setCategory(electCodePackageVos2.get(i).getCategory());
            purchaseReceiptDtlVo.setContractNo(electCodePackageVos2.get(i).getContractNo());
            purchaseReceiptDtlVo.setExpiryDate(electCodePackageVos2.get(i).getExpiryDate());
            purchaseReceiptDtlVo.setIsThirdParty("1");//先默认没有第三方
            purchaseReceiptDtlVo.setLotNumber(electCodePackageVos2.get(i).getLotNumber());
            purchaseReceiptDtlVo.setManufacturerId(electCodePackageVos2.get(i).getManufacturerId());
            purchaseReceiptDtlVo.setManufacturerName(electCodePackageVos2.get(i).getManufacturerName());
            purchaseReceiptDtlVo.setPropertyId(electCodePackageVos2.get(i).getPropertyId());
            purchaseReceiptDtlVo.setQuantity(String.valueOf(electCodePackageVos2.get(i).getQuantity()));
            purchaseReceiptDtlVo.setServicePrice(electCodePackageVos2.get(i).getServicePrice());
            purchaseReceiptDtlVo.setStorage(electCodePackageVos2.get(i).getStorage());
            purchaseReceiptDtlVo.setStorageId(electCodePackageVos2.get(i).getStorageId());
            purchaseReceiptDtlVo.setUnitPrice(electCodePackageVos2.get(i).getUnitPrice());
            purchaseReceiptDtlVo.setVaccineDose(electCodePackageVos2.get(i).getVaccineDose());
            purchaseReceiptDtlVo.setVaccineId(electCodePackageVos2.get(i).getVaccineId());
            purchaseReceiptDtlVo.setVaccineName(electCodePackageVos2.get(i).getVaccineName());
            purchaseReceiptDtlVo.setVaccineLotsId(electCodePackageVos2.get(i).getVaccineLotsId());
            purchaseReceiptDtlVo.setVaccineSpec(electCodePackageVos2.get(i).getVaccineSpec());
            ElectCodePackageVoList electCodePackageVoList = new ElectCodePackageVoList();
            electCodePackageVoList.setCodeLevel(electCodePackageVos2.get(i).getCodeLevel());
            electCodePackageVoList.setElectCode(electCodePackageVos2.get(i).getElectCode());
            electCodePackageVoList.setQuantity(String.valueOf(electCodePackageVos2.get(i).getQuantity()));
            purchaseReceiptDtlVo.setElectCodePackageVoList(electCodePackageVoList);
            purchaseReceiptDtlVoList.add(purchaseReceiptDtlVo);
        }
        purchaseReceiptMstAndDtlVo.setPurchaseReceiptDtlVoList(purchaseReceiptDtlVoList);
        params.put("deptId", application.getDeptId());
        params.put("userId", application.getUserId());
        params.put("purchaseReceiptDtlVoList", purchaseReceiptDtlVoList);
        params.put("purchaseReceiptMstVo", purchaseReceiptMstVo);
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.EMIAO(application.getBackSystem()));
        GetVaccineService vs = retrofit.create(GetVaccineService.class);
        String token = application.getAccessToken();
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),
                JSON.toJSONString(params));
        Call<JsonForResult> jsonForResultCall = vs.savePurchaseReceiptMstAndDtlFromSale(token, requestBody);
        jsonForResultCall.enqueue(new getSavePurchaseReceiptMstAndDtlFromSale());
    }

    private class getSavePurchaseReceiptMstAndDtlFromSale implements Callback<JsonForResult> {

        @Override
        public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
            if (_progressDialog != null) _progressDialog.cancel();
            try {
                JsonForResult jsonForResult = (JsonForResult) response.body();
                if (jsonForResult != null && jsonForResult.isSuccess()) {
                    Toast.makeText(SupplyStorageActivity.this, "入库成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent("com.jhd.test.broadcast");
                    intent.putExtra("type", "1");//1代表入库成功
                    sendBroadcast(intent);
                    onBackPressed();
                } else {
                    if (jsonForResult != null && jsonForResult.getMessage() != null && "".equals(jsonForResult.getMessage()) == false) {
                        UtilHelper.alertMsg(SupplyStorageActivity.this, "错误", jsonForResult.getMessage());
                    }
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<JsonForResult> call, Throwable t) {
            String msg = "网络异常!";
            if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
            UtilHelper.alertMsg(SupplyStorageActivity.this, "错误", msg);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnCountVaccine://跳转到交货单明细页面
                startActivity(new Intent(SupplyStorageActivity.this, SupplyDetailActivity.class));
                break;
            case R.id.btnSubmitVaccine://进行提交处理,最好先弹窗提示一下
                if (electCodePackageVos2.size() > 0) {
                    int delNum = 0;
                    int scanNum = 0;
                    for (int i = 0; i < deliveryDtlList.size(); i++) {
                        delNum = delNum + deliveryDtlList.get(i).getQuantity();
                        int num = 0;
                        for (int j = 0; j < electCodePackageVos2.size(); j++) {
                            if (electCodePackageVos2.get(j).getLotNumber().equals(deliveryDtlList.get(i).getLotNumber())) {
                                num = num + electCodePackageVos2.get(j).getQuantity();
                                deliveryDtlList.get(i).setScan(num);
                            }
                        }
                        scanNum = scanNum +num;
                    }
                    if (delNum == scanNum) {
                        showDialog(1, v, "汇总正常");
                    } else {
                        showDialog(1, v, "汇总不符");
                    }
                } else {
                    Toast.makeText(SupplyStorageActivity.this, "你还没有扫描任何疫苗", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.btnDelAllVaccine://删除本地缓存的扫描数据,最好先弹窗提示操作
                showDialog(2, v, "清空");
                break;
        }
    }

    public void showDialog(int dex, View v, String title) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(v.getContext());
        alertBuilder.setTitle(title);
        if(title.equals("汇总不符")){
            alertBuilder.setIcon(R.mipmap.dialog_alert);
            int delNum = 0;
            int scanNum = 0;
            String msg="";
            for (int i = 0; i < deliveryDtlList.size(); i++) {
                delNum = deliveryDtlList.get(i).getQuantity();
                int num = 0;
                for (int j = 0; j < electCodePackageVos2.size(); j++) {
                    if (electCodePackageVos2.get(j).getLotNumber().equals(deliveryDtlList.get(i).getLotNumber())) {
                        num = num + electCodePackageVos2.get(j).getQuantity();
                        deliveryDtlList.get(i).setScan(num);
                    }
                }
                scanNum =num;
                if(scanNum!=delNum){
                    msg="疫苗名称:"+deliveryDtlList.get(i).getVaccineName()+" 交货单数量:"+delNum+" 扫码数量"+scanNum+"\n";
                }
            }
            alertBuilder.setMessage("扫描累计与原交货单不符,"+msg+"是否作为部分入库仍然保存?");
        }else if(title.equals("汇总正常")){
            alertBuilder.setMessage("交货单的疫苗均已扫描,确定全部入库吗?");
        }else if(title.equals("清空")){
            alertBuilder.setIcon(R.mipmap.dialog_alert);
            alertBuilder.setMessage("你确定清空所有录入的疫苗追溯码吗");
        }else if(title.equals("选择储位")){
            alertBuilder.setMessage("你确定选择该储位:"+application.getStorageName()+" 进行入库吗?");
        }
        alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (dex == 1) {//保存所有数据
                    savePurchaseReceiptMstAndDtlFromSale();
                } else if (dex == 2) {//清空储位数据
                    electCodePackageVos.clear();
                    electCodePackageVos2.clear();
                    getDelivery();
                    adapter = new RecyclerAdapter(SupplyStorageActivity.this, list);
                    recyclerView.setAdapter(adapter);
                    //将content的界面刷新掉
                    Intent intent = new Intent("com.jhd.test.broadcast");
                    intent.putExtra("type", "2");//2代表清空扫描
                    sendBroadcast(intent);
                }else if(dex==3){
                    SupplyStorageActivity.this.finish();
                }
                _alertDialog.dismiss();
            }
        });

        alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                _alertDialog.dismiss();
            }
        });

        _alertDialog = alertBuilder.create();
        _alertDialog.show();
    }

    private class getStorageInfoListCallback implements Callback<JsonForResult> {

        @Override
        public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
            if (_progressDialog != null) _progressDialog.cancel();
            try {
                JsonForResult jsonForResult = (JsonForResult) response.body();
                if (jsonForResult != null && jsonForResult.isSuccess() && jsonForResult.getData() != null) {
                    List<StorageInfo> ds = JSON.parseArray(jsonForResult.getData().toString(), StorageInfo.class);
                    for (int i = 0; i < ds.size(); i++) {
                        list.add(ds.get(i));
                    }
                    adapter = new RecyclerAdapter(SupplyStorageActivity.this, list);
                    recyclerView.setAdapter(adapter);
                } else {
                    if (jsonForResult != null && jsonForResult.getMessage() != null && "".equals(jsonForResult.getMessage()) == false) {
                        UtilHelper.alertMsg(SupplyStorageActivity.this, "错误", jsonForResult.getMessage());
                    }
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<JsonForResult> call, Throwable t) {
            if (_progressDialog != null) _progressDialog.cancel();
            String msg = "网络异常!";
            if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
            UtilHelper.alertMsg(SupplyStorageActivity.this, "错误", msg);
        }
    }


    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
        private List<StorageInfo> storageList;

        public RecyclerAdapter(SupplyStorageActivity supplyStorageActivity, List<StorageInfo> storageList) {
            this.storageList = storageList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_storage2, parent, false);
            ViewHolder viewHolder = new ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.storageName.setText(storageList.get(position).getName());
            holder.brand.setText(storageList.get(position).getBrand());
            int num = 0;
            for (int i = 0; i < electCodePackageVos2.size(); i++) {
                if (electCodePackageVos2.get(i).getPosition() == position) {
                    num = num + electCodePackageVos2.get(i).getQuantity();
                }
                holder.num.setText(String.valueOf(num));
            }
            if(application.getPosition()==position){
                holder.storageName.setTextColor(getResources().getColor(R.color.colorBlue));
                holder.brand.setTextColor(getResources().getColor(R.color.colorBlue));
                holder.brand2.setTextColor(getResources().getColor(R.color.colorBlue));
                holder.num2.setTextColor(getResources().getColor(R.color.colorBlue));
                holder.rlStorage.setBackgroundResource(R.drawable.storage_background_2);
            }else{
                if (num != 0) {
                    holder.rlStorage.setBackgroundResource(R.drawable.storage_background_1);
                } else {
                    holder.rlStorage.setBackgroundResource(R.drawable.storage_background_3);
                }
                holder.storageName.setTextColor(getResources().getColor(R.color.colorBlack));
                holder.brand.setTextColor(getResources().getColor(R.color.colorText));
                holder.brand2.setTextColor(getResources().getColor(R.color.colorText));
                holder.num2.setTextColor(getResources().getColor(R.color.colorText));
            }
            holder.rlStorage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    application.setStorageName(storageList.get(position).getName());
                    application.setStorageId(storageList.get(position).getId());
                    application.setPosition(position);
                    notifyDataSetChanged();
                    showDialog(3,v,"选择储位");
                }
            });
//
        }

        @Override
        public int getItemCount() {
            return storageList.size();
        }
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView storageName, brand, num,brand2,num2; //储位名称，温度，品牌，入库数
            RelativeLayout rlStorage;
            public ViewHolder(View itemView) {
                super(itemView);
                this.storageName = itemView.findViewById(R.id.tvStorage);
                this.brand = itemView.findViewById(R.id.tv_Device);
                this.brand2 = itemView.findViewById(R.id.tv_Device2);
                this.num = itemView.findViewById(R.id.tv_InNo);
                this.num2 = itemView.findViewById(R.id.tv_InNo2);
                this.rlStorage = itemView.findViewById(R.id.rlStorage);
            }
        }
    }
}